//
// Created by RandBII on 2020/12/27.
//

#ifndef FFMPEGFORANDROID_VIDEODECODER_H
#define FFMPEGFORANDROID_VIDEODECODER_H

#include "../DecoderBase.h"
#include "VideoRender.h"

extern "C" {
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
}


class VideoDecoder : public DecoderBase {

public:
    VideoDecoder(char *url);

    virtual ~VideoDecoder();

    int GetVideoWidth();

    int GetVideoHeight();

    void SetVideoRender(VideoRender *videoRender);

    float GetCurrentPosition();

    static long GetVideoDecoderForTimestampAVSync(void *context);

private:
    const AVPixelFormat DST_PIXEL_FORMAT = AV_PIX_FMT_RGBA;
    int m_videoWidth = 0;
    int m_videoHeight = 0;
    int m_RenderWidth = 0;
    int m_RenderHeight = 0;
    AVFrame *m_RGBAFrame = nullptr;
    uint8_t *m_FrameBuffer = nullptr;

    VideoRender *m_VideoRender = nullptr;
    SwsContext *m_SwsContext = nullptr;

    virtual void OnDecoderReady();

    virtual void OnDecoderDone();

    virtual void OnFrameAvailable(AVFrame *avFrame);

};


#endif //FFMPEGFORANDROID_VIDEODECODER_H
